
//=======================================
// FIGURE 2 [RUN AFTER USEP_did.do]
//=======================================
global DATA  "path/to/input/data"   
global OUT "path/to/output/folder"
global INT "path/to/intermediate/files"

forvalues seed=1/1000 {
	use "$INT\twin_USEP_appended_applicant_did_prep_`seed'.dta", clear
	local policy_date=14943
	local window_size=7
	local window_num=26
	gen time=floor((US_filing_date_d_cited-`policy_date')/`window_size') + 0.5
	gen count=1
	collapse (mean) lag (sum) count, by(nonJP time twin)

	foreach t in 0 1 {
		sum lag if nonJP==1 & twin==`t' & time<0 [fweight=count]
		gen US_mean_`t'=r(mean) if time<0 & twin==`t'
		sum lag if nonJP==1 & time>=0 & twin==`t' [fweight=count]
		replace US_mean_`t'=r(mean) if time>=0 & twin==`t'
		sum lag if nonJP==0 & time<0 & twin==`t' [fweight=count]
		gen JP_mean_`t'=r(mean) if time<0 & twin==`t'
		sum lag if nonJP==0 & time>=0 & twin==`t' [fweight=count]
		replace JP_mean_`t'=r(mean) if time>=0 & twin==`t'
	}
	save "$INT\twin_USEP_appended_applicant_graph_prep_`seed'.dta", replace
	}

clear
forvalues seed=1/1000 {
	append using "$INT\twin_USEP_appended_applicant_graph_prep_`seed'.dta"
}

collapse (mean) lag US_mean* JP_mean* , by(nonJP twin time)

set scheme tufte
local us_color="blue"
local jp_color="orange_red"

sort time nonJP

tw (sc lag time if nonJP==1 , color(`us_color') m(diamond_hollow) leg(lab(1 US inventors))) (sc lag time if nonJP==0, color(`jp_color') m(circle) leg(lab(2 JP inventors))) (line US_mean_0 time, color(`us_color') lp(dash) leg(lab(3 US mean))) (line JP_mean_0 time, color(`jp_color') lp(dash) leg(lab(4 JP mean))) if twin==0,  xline(0, lwidth(thin) lcolor(gray)) yti(log(lag)) xti(US application date (weeks relative to AIPA implementation)) xlabel(-26 0 26) ylabel(6 6.5 7) leg(cols(2))
save "$OUT\twincites_applicant_graph_USEP.dta", replace	
graph export "$OUT\parallel_trend_applicant_USEP.pdf", as(pdf) name("Graph") replace





